import javax.json.Json;
import DTO.userinfomation;
import javax.json.bind.Jsonb;
import javax.json.bind.JsonbBuilder;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.sql.*;
@WebServlet("/register_servlet")
public class register_servlet extends HttpServlet {
    private static final String URL = "jdbc:h2:~/jpa-demo-db;MODE=MYSQL;DB_CLOSE_DELAY=-1";
    private static final String NAME = "sa";
    private static final String PASSWORD = "sa";
    private static final EntityManagerFactory entityManagerFactory;
    private static final String PERSISTENCE_UNIT_NAME = "MyJPADemoPU";
    static {
        entityManagerFactory = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME);
    }
    private EntityManager getEntityManager() {
        return entityManagerFactory.createEntityManager();
    }

    protected void doPost(HttpServletRequest req,HttpServletResponse resp) throws ServletException, IOException{
        doGet(req,resp);
    }
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException,IOException{
        int flag = 0;
        String username = req.getParameter("username");
        String phone = req.getParameter("phone");
        String password1 = req.getParameter("password");
        String password2 = req.getParameter("password_enter");
        String img = "default.jpg";
        if(!password1.equals(password2)) {
            resp.sendRedirect("error.html");
        }
        else {
            try {
                Connection conn = DriverManager.getConnection(URL, NAME, PASSWORD);
                Statement stmt = conn.createStatement();
                ResultSet rs = stmt.executeQuery("select * from USERINFOMATION ");
                while (rs.next()) {
                    String username_check = rs.getString("USERNAME");
                    if (username_check.equals(username)) {
                        flag = 1;
                        resp.setContentType("text/html;charset=UTF-8");
                        resp.getWriter().println("用户名已被使用，请选择其他用户名!");
                        resp.getWriter().println("<br>");
                        resp.getWriter().println("<input type = submit value = 点此重新注册 onclick = location='register.jsp'>");
                        break;
                    }
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }

            if (flag != 1) {  //如果用户注册的用户名没有重复 则进行下列操作
                EntityManager em = getEntityManager();
                EntityTransaction tx = em.getTransaction();

                try {
                    userinfomation user = new userinfomation(username, password1, phone, img);
                    tx.begin();
                    em.persist(user);
                    tx.commit();
                } catch (Exception e) {
                    throw new ServletException(e);
                } finally {
                    em.close();
                }
                resp.sendRedirect("login1.jsp");
            }
        }
    }
}
